package com.jxc.jxc01.hch.web;

import com.alibaba.fastjson2.JSONObject;
import com.jxc.jxc01.hch.entity.*;
import com.jxc.jxc01.hch.service.IWareHouseService;
import com.jxc.jxc01.hch.service.Impl.IWareHouseServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("*.WareHouse")
public class WareHouseServlet extends HttpServlet {
    IWareHouseService wareHouseService = new IWareHouseServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String uri = req.getRequestURI();
        System.out.println(uri);
        switch (uri){
            case "/jxc01/list.WareHouse":
                list(req,resp);
                break;
            case "/jxc01/update.WareHouse":
                update(req,resp);
                break;
            case "/jxc01/delete.WareHouse":
                delete(req,resp);
                break;
            case "/jxc01/increase.WareHouse":
                increase(req,resp);
                break;
            case "/jxc01/query.WareHouse":
                query(req,resp);
                break;

        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        try {
            WareHouse wareHouse = new WareHouse();
            wareHouse.setId(Integer.parseInt(req.getParameter("id")));
            wareHouse.setName(req.getParameter("name"));
            wareHouse.setHeadname(req.getParameter("headname"));
            wareHouse.setPhone(req.getParameter("phone"));
            wareHouse.setAddress(req.getParameter("address"));
            wareHouse.setSize(Integer.parseInt(req.getParameter("size")));
            wareHouse.setUnit(req.getParameter("unit"));
            wareHouse.setState(req.getParameter("state"));
            wareHouse.setNotes(req.getParameter("notes"));
            wareHouseService.modify(wareHouse);
            resp.sendRedirect("cangkuliebiao.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            int id = Integer.parseInt(req.getParameter("id"));
            wareHouseService.delete(id);
            resp.sendRedirect("cangkuliebiao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void query(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageHelper<WareHouse> pageHelper = new PageHelper<>();
            PrintWriter out = resp.getWriter();
            //获取页码页容
//            int pageNumber = Integer.parseInt(req.getParameter("pagenumber"));
//            int pageSize = Integer.parseInt(req.getParameter("pagesize"));
            int pageNumber=1;
            int pageSize=10;
            String name = req.getParameter("name");
            pageHelper.setPageNum(pageNumber);
            pageHelper.setPageSize(pageSize);
            pageHelper = wareHouseService.query(pageHelper,name);
            HttpSession session = req.getSession();
            session.setAttribute("query",pageHelper);
            RespResult respResult = new RespResult(0,"ok",pageHelper.getTotal(),pageHelper.getData());
            out.print(JSONObject.toJSONString(respResult));
            resp.sendRedirect("cangkuliebiao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void increase(HttpServletRequest req, HttpServletResponse resp) {
        try {
            WareHouse wareHouse = new WareHouse();
            wareHouse.setName(req.getParameter("name"));
            wareHouse.setHeadname(req.getParameter("headname"));
            wareHouse.setPhone(req.getParameter("phone"));
            wareHouse.setAddress(req.getParameter("address"));
            wareHouse.setSize(Integer.parseInt(req.getParameter("size")));
            wareHouse.setUnit(req.getParameter("unit"));
            wareHouse.setState(req.getParameter("state"));
            wareHouse.setNotes(req.getParameter("notes"));
            wareHouseService.increase(wareHouse);
            resp.sendRedirect("cangkuliebiao.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageHelper<WareHouse> pageHelper = new PageHelper<>();
            PrintWriter out = resp.getWriter();
            //获取页码页容
            int pageNumber = Integer.parseInt(req.getParameter("pagenumber"));
            int pageSize = Integer.parseInt(req.getParameter("pagesize"));
            pageHelper.setPageNum(pageNumber);
            pageHelper.setPageSize(pageSize);
            HttpSession session = req.getSession();
            if (session.getAttribute("query")==null) {
                pageHelper = wareHouseService.list(pageHelper);
            }else {
                pageHelper= (PageHelper<WareHouse>) session.getAttribute("query");
            }
            RespResult respResult = new RespResult(0,"ok",pageHelper.getTotal(),pageHelper.getData());
            out.print(JSONObject.toJSONString(respResult));
            session.removeAttribute("query");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
